﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using Cosmetic.DataAccess;
using Cosmetic.DataEntity;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Cosmetic.Logic
{
   public  class CardNumbersManger:LogicBase
    {
       private cardnumber m_cardnumber = new cardnumber();
       /// <summary>
       /// 新增一个卡号
       /// </summary>
       /// <param name="anumber">The product want delete</param>

       public void Insert(cardnumber anumber)
       {
           m_CosmeticDC.cardnumber .InsertOnSubmit(anumber);
           m_CosmeticDC.SubmitChanges();
       }

       /// <summary>
       /// 删除一个卡号
       /// </summary>
       /// <param name="aProduct">The product want delete</param>
       public void Delete(cardnumber anumber)
       {
           m_CosmeticDC.cardnumber.DeleteOnSubmit(anumber);
           m_CosmeticDC.SubmitChanges();
       }
       /// <summary>
       /// 查询所有的卡号
       /// </summary>
       /// <param name="anumber">The product want delete</param>

       public List<cardnumber > GetAll()
       {
           return (from theActivity in m_CosmeticDC.cardnumber
                   select theActivity).ToList();
       }

       /// <summary>
       /// 查询所有的卡号
       /// </summary>
       /// <param name="anumber">The product want delete</param>

       public IEnumerable GetAllnumbers()
       {
           var result = from theFocus in m_CosmeticDC.numbers
                        join theProduct in m_CosmeticDC.Users
                        on theFocus.userid equals theProduct.UserId
                     
                        select new
                        {
                            id = theFocus.id,
                            //name = theProduct.UserName,
                           
                            isuserd = theFocus.IsUsed,
                            number = theFocus.number,
                            ynumber = theFocus.ynumber,

                        };
           return result;
       }

       /// <summary>
       /// 判断是否存在次卡号
       /// </summary>
       /// <param name="anumber">The product want delete</param>

       public bool CheckExists(string uername)
       {
           List<cardnumber> theProductList = (from theProduct in m_CosmeticDC.cardnumber
                                           where theProduct.cardnumbers == uername
                                           select theProduct).ToList();
           return theProductList.Count > 0;
       }
       /// <summary>
       /// 判断是否存在次卡号
       /// </summary>
       /// <param name="anumber">The product want delete</param>

       public cardnumber CheckExistes(string uername)
       {
           List<cardnumber> theProductList = (from theProduct in m_CosmeticDC.cardnumber
                                           where theProduct.cardnumbers == uername
                                           select theProduct).ToList();
           if (theProductList.Count == 1)
           {
               return theProductList[0];
           }
           return null;
       }
       /// <summary>
       /// 根据ID获得所有的结果
       /// </summary>
       /// <param name="anumber">The product want delete</param>

       public cardnumber GetProductById(int aProductId)
       {
           return m_CosmeticDC.cardnumber.Single(theProect => theProect.id == aProductId);
       }


       /// <summary>
       /// 根据用户ID获得所有的结果
       /// </summary>
       /// <param name="anumber">The product want delete</param>

       //public cardnumber GetnumbersByuserid(string userid)
       //{
       //    List<cardnumber > theUserList = (from theUser in m_CosmeticDC.cardnumber
       //                                 where theUser.cardnumbers == userid
       //                                 select theUser).ToList();
       //    if (theUserList.Count == 1)
       //    {
       //        return theUserList[0];
       //    }
       //    return null;
       //}



       //public cardnumber GetProductById(int  id)
       //{
       //    return m_CosmeticDC.cardnumber.Single(theProect => theProect.id  == id);
       //}
       public cardnumber GetCashCard(string CardNum)
       {
           var result = (from card in m_CosmeticDC.cardnumber
                         where card.cardnumbers == CardNum
                         select card);
           return result.Single();
       }


       public static void UpdateInformation(int id,bool a)
       {
           string conStr = ConfigurationManager.ConnectionStrings["CosmeticConnectionString"].ConnectionString;
           SqlConnection con = new SqlConnection(conStr);
           try
           {
               con.Open();
               string cmdStr = string.Empty;

               cmdStr = "UPDATE [cardnumber]  SET [Isuserd] ='" + a + "'  WHERE id=" + id + "";
               SqlCommand com = new SqlCommand(cmdStr, con);
               com.ExecuteNonQuery();
           }
           finally
           {
               con.Close();
           }
       }
    }

}

